import 'package:flutter/material.dart';

///定义枚举类型区别 type
enum SearchBarType2 { home, homeLight, normal }

///搜索框
class SearchBar2 extends StatefulWidget {
  //是否允许输入
  final bool enable;

  //是否隐藏左边按钮
  final bool hideLeft;
  final SearchBarType2 searchBarType;

  //默认没有文字 显示的hint文字
  final String hint;

  //默认显示的文字
  final String defaultText;

  //左边按钮点击
  final void Function() leftButtonClick;

  //右边按钮点击
  final void Function()? rightButtonClick;

  //语言按钮点击
  final void Function() speakClick;

  //输入框点击
  final void Function() inputBoxClick;

  final ValueChanged<String> onChanged;

  const SearchBar2(
      {Key? key,
      required this.enable,
      required this.hideLeft,
      required this.searchBarType,
      required this.hint,
      required this.defaultText,
      required this.leftButtonClick,
      this.rightButtonClick,
      required this.speakClick,
      required this.inputBoxClick,
      required this.onChanged})
      : super(key: key);

  @override
  State<SearchBar2> createState() => _SearchBar2State();
}

class _SearchBar2State extends State<SearchBar2> {
  //文字监听控制器
  TextEditingController _controller = TextEditingController();

  //是否显示清除按钮
  bool showClear = false;

  @override
  void initState() {
    super.initState();
    if (widget.defaultText.isNotEmpty) {
      _controller.text = widget.defaultText;
    }
  }

  @override
  Widget build(BuildContext context) {
    return widget.searchBarType == SearchBarType2.home
        ? _genHomeSearch()
        : _genNormalSearch();
  }

  //首页搜索布局
  _genHomeSearch() {}

  //搜索也搜索布局
  _genNormalSearch() {
    return Row(
      children: [
        _wrapTap(
            Container(
              padding: EdgeInsets.fromLTRB(6, 5, 10, 5),
              child: widget.hideLeft
                  ? const Icon(
                      Icons.arrow_back_ios,
                      color: Colors.grey,
                      size: 26,
                    )
                  : null,
            ),
            widget.leftButtonClick),
        Expanded(
          flex: 1,
          child: _inputBox(),
        ),
        _wrapTap(
            Container(
              padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
              child: const Text(
                '搜索',
                style: TextStyle(color: Colors.blue, fontSize: 17),
              ),
            ),
            widget.rightButtonClick!)
      ],
    );
  }

  //输入框布局
  _inputBox() {
    Color inputBoxColor;
    if (widget.searchBarType == SearchBarType2.home) {
      inputBoxColor = Colors.white;
    } else {
      inputBoxColor = Color(int.parse("0xffededed"));
    }
    return Container(
      height: 30,
      padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
      decoration: BoxDecoration(
        color: inputBoxColor,
        borderRadius: BorderRadius.circular(
            widget.searchBarType == SearchBarType2.home ? 10 : 5),
      ),
      child: Row(
        children: [
          Icon(
            Icons.search,
            size: 20,
            color: widget.searchBarType == SearchBarType2.home
                ? Colors.blue
                : const Color(0xffa9a9a9),
          ),
          Expanded(
              flex: 1,
              child: widget.searchBarType == SearchBarType2.home
                  ? _wrapTap(
                      Container(
                        child: Text(
                          widget.defaultText,
                          style: const TextStyle(fontSize: 15),
                        ),
                      ),
                      widget.inputBoxClick)
                  : TextField(
                      controller: _controller,
                      onChanged: _onChanged,
                      autofocus: true,
                      style: const TextStyle(
                          fontSize: 18.0,
                          color: Colors.black,
                          fontWeight: FontWeight.w300),
                      //输入文本的样式
                      decoration: InputDecoration(
                          contentPadding: const EdgeInsets.only(
                              left: 5, bottom: 14, right: 5),
                          border: InputBorder.none,
                          hintText: widget.hint,
                          hintStyle: const TextStyle(fontSize: 15)),
                    )),
          showClear
              ? _wrapTap(
                  const Icon(
                    Icons.clear,
                    size: 22,
                    color: Colors.grey,
                  ), () {
                  setState(() {
                    _controller.clear();
                  });
                  _onChanged("");
                })
              : _wrapTap(
                  Icon(
                    Icons.mic,
                    size: 22,
                    color: widget.searchBarType == SearchBarType2.home
                        ? Colors.grey
                        : Colors.blue,
                  ),
                  widget.speakClick)
        ],
      ),
    );
  }

  _onChanged(String text) {
    if (text.isNotEmpty) {
      setState(() {
        showClear = true;
      });
    } else {
      setState(() {
        showClear = false;
      });
    }
    if (widget.onChanged != null) {
      widget.onChanged(text);
    }
  }

  ///点击事件
  _wrapTap(Widget child, void Function() callback) {
    return GestureDetector(
      onTap: () {
        if (callback != null) {
          callback();
        }
      },
      child: child,
    );
  }
}
